package com.ihelp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.interfaces.MPJBaseJoin;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ihelp.pojo.Order;
import com.ihelp.mapper.OrderMapper;
import com.ihelp.pojo.OrderDTO;
import com.ihelp.pojo.User;
import com.ihelp.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ihelp.util.MyWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiuZiMing
 * @since 2023-05-23
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<OrderDTO> getAllOrder() {
        return orderMapper.selectJoinList(OrderDTO.class, MyWrapper.allOrderWrapper());
    }

    @Override
    public OrderDTO getOrderById(Integer id) {
        MPJLambdaWrapper<Order> mpjLambdaWrapper = MyWrapper.allOrderWrapper();
        MPJLambdaWrapper<Order> eq = mpjLambdaWrapper.eq("t.id", id);
        return orderMapper.selectJoinOne(OrderDTO.class, eq);
    }

    @Override
    public List<OrderDTO> getOrderByDynamicConditions(Integer id, Integer visitorId, Integer accompanyerId, String orderTime,
                                                      Double duration, Integer hospitalId, String visitOrder, String visitorEvaluate,
                                                      String accompanyerEvaluate, Integer serviceTypeId, Integer orderStatusId,
                                                      Integer visitorEvaluateLevelId, Integer accompanyerEvaluateLevelId) {
        MPJLambdaWrapper<Order> orderQueryWrapper = MyWrapper.allOrderWrapper();
        for (int i = 0; i < 1; i++) {
            if (id != null && id > 0) {
                orderQueryWrapper.eq("id", id);
            }
            if (visitorId != null) {
                orderQueryWrapper.eq("visitor_Id", visitorId);
            }
            if (accompanyerId != null) {
                orderQueryWrapper.eq("accompanyer_id", accompanyerId);
            }
            if (orderTime != null) {
                orderQueryWrapper.eq("order_time", orderTime);
            }
            if (duration != null) {
                orderQueryWrapper.eq("duration", duration);
            }
            if (hospitalId != null) {
                orderQueryWrapper.eq("hospital_id", hospitalId);
            }
            if (visitOrder != null) {
                orderQueryWrapper.eq("visit_order", visitOrder);
            }
            if (visitorEvaluate != null) {
                orderQueryWrapper.eq("visitor_evaluate", visitorEvaluate);
            }
            if (accompanyerEvaluate != null) {
                orderQueryWrapper.eq("accompanyer_evaluate", accompanyerEvaluate);
            }
            if (serviceTypeId != null) {
                orderQueryWrapper.eq("service_type_id", serviceTypeId);
            }
            if (orderStatusId != null) {
                orderQueryWrapper.eq("order_status_id", orderStatusId);
            }
            if (visitorEvaluateLevelId != null) {
                orderQueryWrapper.eq("visitor_evaluate_level_id", visitorEvaluateLevelId);
            }
            if (accompanyerEvaluateLevelId != null) {
                orderQueryWrapper.eq("accompanyer_evaluate_level_id", accompanyerEvaluateLevelId);
            }
        }

        List<OrderDTO> orderList = orderMapper.selectJoinList(OrderDTO.class, orderQueryWrapper);
        return orderList;
    }

    @Override
    public int createOrder(Integer visitorId, Integer accompanyerId, String orderTime, String visitTime,
                           Double duration, Integer serviceTypeId, String visitInformation, Integer hospitalId,
                           Integer hospitalDepartmentId) {
        Order order = new Order();
        order.setVisitorId(visitorId);
        order.setAccompanyerId(accompanyerId);
        order.setOrderTime(orderTime);
        order.setVisitTime(visitTime);
        order.setDuration(duration);
        order.setServiceTypeId(serviceTypeId);
        order.setVisitInformation(visitInformation);
        order.setHospitalId(hospitalId);
        order.setHospitalDepartmentId(hospitalDepartmentId);
        return orderMapper.insert(order);
    }


}
